{include mobile_template('header')}
<link rel="stylesheet" href="{URLROOT}/{$_G[tplurl_mobile]}/css/party.css" />
<div class="panel-comm panel-default panel-fullsize">
    <div class="panel-heading">
        <h1>[精彩照片] $detail[subject]</h1>
        <p class="txt-small">
            <span>发起人：$detail[username]</span>
            <span class="split"></span>
            <span>发起时间：{date $detail[dateline],'w2style'}</span>
        </p>
    </div>

    <div class="panel-body">
        <div class="btn-group-vertical full-width mb10">
            <!--{if $user->isLogin}-->
            <a class="btn btn-mycolor" href="javascript:" id="post_comment_btn"><span>我要咨询</span></a>
            <!--{else}-->
            <a class="btn btn-warning" href="{url member/mobile/do/login}"><span>咨询活动，请先登录</span></a>
            <!--{/if}-->
        </div>
        <ul class="party-comment-list" id="data_container">
            {include mobile_template('party_comments_loop')}
        </ul>
        <div id="page_container"></div>
    </div>
</div>
<div class="form-box none" style="margin:10px;" id="frm_comment">
    <form action="{url party/mobile/do/detail/id/$partyid/op/comments}" method="post">
        <div class="form-item">
            <label>咨询内容：</label>
            <textarea name="message" placeholder="请输入咨询内容" required></textarea>
        </div>
        <!--{if $MOD['comment_seccode']}-->
        <div class="form-item">
            <label>验证码：</label>
            <div>
                <input style="width:60%;" name="seccode" placeholder="请输入右侧验证码" type="text" required>
                <span data-name="seccode"></span>
            </div>
        </div>
        <!--{/if}-->
        <div class="form-item">
            <div class="btn-group btn-group-auto">
                <div class="btn-group">
                    <a class="btn btn-mycolor" href="javascript:" data-type="submit"><span>提交</span></a>
                </div>
                <div class="btn-group">
                    <a class="btn btn-default" href="javascript:" data-type="close"><span>关闭</span></a>
                </div>
            </div>
        </div>
    </form>
</div>
<script>
var mdrawer
$(document).ready(function() 
{
    //分页插件
    $('#page_container').m_ajaxpage({
        //load_mod:'override',
        container:'#data_container'
    });

    //加载对话框
    $('#post_comment_btn').click(function() {
        if(!mdrawer) {
            mdrawer = new $.m_drawer('#frm_comment', {}, {
                onInit:function(jq, workArea) {
                    $('#frm_comment').show();
                    workArea.find('[data-name="seccode"]').m_seccode();
                },
                onClose:function(jq, workArea) {
                    var text = workArea.find('[name=message]').val().trim();
                    if(text!='') {
                        var truthBeTold = window.confirm('咨询内容尚未提交，是否关闭？');
                        if(truthBeTold) workArea.find('form')[0].reset();
                        return truthBeTold;
                    }
                },
                onSubmit:function(jq, workArea) {
                    comment_submit();
                }
            });
        }
        mdrawer.open();
    });

});

function comment_check(frm)
{
    var error = '';
    var post = {};

    var textarea = frm.find('[name="message"]');
    post.message = encodeURIComponent(textarea.val().trim());
    if(post.message == '') {
        error += "\n"+textarea.attr('placeholder');
    }

    var seccode = frm.find('[name="seccode"]');
    if(seccode[0]) {
        post.seccode = seccode.val().trim();
        if(post.seccode == '') {
            error += "\n"+seccode.attr('placeholder');
        }
    }

    if(error!='') {
        alert("表单提交未通过，请完成下列操作：\n"+error);
        return false;
    }

    return post;
}

function comment_submit()
{
    var frm = $('#frm_comment form');

    var post = comment_check(frm);
    if(!post) return;

    post.in_ajax = 1;
    post.dosubmit = 'post_comment';

    var url = frm.attr('action').url();
    $.post(url, post, function(data)
    {
        if(is_message(data)) {
            myAlert(data);
        } else if(is_json(data)) {
            result = parse_json(data);
            if(result.code != 200) {
                alert("Error\ncode:"+result.code+"\nmessage:"+result.message);
            } else {
                frm[0].reset(); //重置表单
                mdrawer.close(); //关闭弹出层表单
                insert_comment(result.data);
                $(body).animate({scrollTop:0},350);
                window.setTimeout(function() {alert('提交成功！');}, 350);
            }
        } else {
            alert('未知错误.');
        }
    });
}

function insert_comment(data)
{
    var li = $('<li data-id="'+data.commentid+'"></li>');
    var question = $(
        '<div class="question">'+
            '<p class="content">'+data.message+'</p>'+
            '<p class="info">'+
                '<span class="author">'+data.username+'</span>'+
                '<span class="posttime">刚刚</span>'+
            '</p>'+
        '</div>');
    var answer = $(
        '<div class="answer">'+
            '<p class="content"><span class="noreply">发起人尚未回复</span></p>'+
        '</div>');
    $('#data_container').prepend(li.append(question).append(answer));
}

function reply_submit(frm)
{

}
</script>

{include mobile_template('footer')}